package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import util.JDBCUtil;
import domain.Message;
import ex.DatabaseException;

public class MessageDAO {
	private Connection conn;
	private PreparedStatement stmt;
	private ResultSet rs;
	
	private static final String SELECT_MESSAGE_BY_UID1_AND_UID2="select * from mesagge where (uidS= ? OR uidS= ?) AND (uidR= ? OR uidR= ?)";
	private static final String SELECT_MESSAGE_BY_UID="select * from mesagge where uidS= ? OR uidR= ?";
	private static final String CREATE_MESSAGE="insert into mesagge (uidS, uidR, content) values (?,?,?) ";

	//this method extracts all the messages between two users
	public LinkedList<Message> selectMessageByUid1AndUid2(long uid1, long uid2) throws DatabaseException{
		conn = JDBCUtil.getConnection();
		String content = null;
		Timestamp timestamp = null;
		LinkedList<Message> messages = new LinkedList<Message>();
		try{
			stmt = conn.prepareStatement(SELECT_MESSAGE_BY_UID1_AND_UID2);
			stmt.setLong(1, uid1);
			stmt.setLong(2, uid2);
			stmt.setLong(3, uid1);
			stmt.setLong(4, uid2);
			rs = stmt.executeQuery();
			if (rs.next()) {
				uid1 = rs.getLong("uidS");
				uid2 = rs.getLong("uidR");
				content = rs.getString("content");
				timestamp = rs.getTimestamp("timestamp");
				messages.add(new Message(uid1,uid2,timestamp,content));
			}
			stmt.close();
			JDBCUtil.closeConnection(conn);		
		}catch (SQLException e) {
			throw new DatabaseException("Problem with retrieving messages between two users! "+ e.getMessage());
		}	
		return messages;		
	}
	
	//this method extracts all the users that a particular user has contacted
	public Map<Long, Timestamp> selectMessageByUid(long uid) throws DatabaseException{
		conn = JDBCUtil.getConnection();
		long uid2 = -1;
		Timestamp timestamp = null;
		Map<Long,Timestamp> messages = new HashMap<Long,Timestamp>();
		//TODO assumption is that we get rs in increasing order by timestamp, so check
		try{
			stmt = conn.prepareStatement(SELECT_MESSAGE_BY_UID);
			stmt.setLong(1, uid);
			stmt.setLong(2, uid);
			rs = stmt.executeQuery();
			if (rs.next()) {
				uid2 = rs.getLong("uidR");
				if(uid2 == uid)
					uid2 = rs.getLong("uidS");
				timestamp = rs.getTimestamp("timestamp");
				messages.put(uid2, timestamp);
			}
			stmt.close();
			JDBCUtil.closeConnection(conn);		
		}catch (SQLException e) {
			throw new DatabaseException("Problem with retrieving all the conversations that a user has made!"	+ e.getMessage());
		}
		return messages;		
	}
	
	//this method inserts a new message
	public void insertMessage(Message msg) throws DatabaseException{
		conn = JDBCUtil.getConnection();
		try {
			stmt = conn.prepareStatement(CREATE_MESSAGE);
			stmt.setLong(1, msg.getUidS());
			stmt.setLong(2, msg.getUidR());
			stmt.setString(3, msg.getContent());
			stmt.execute();
			stmt.close();
			JDBCUtil.closeConnection(conn);
		}catch (SQLException e) {
			throw new DatabaseException("Problem with inserting message! "	+ e.getMessage());
		}			
		
	}
	
}
